<?php

namespace catchAdmin\api\controller;

use catcher\Code;
use thans\jwt\facade\JWTAuth;
use think\facade\Db;
use think\Request;
use catcher\CatchResponse;
/*
 * 海关编码接口
 * */
class Customscode extends Index
{
    /**
     * 获取token
     * @param Request $request
     * @return string
     */
    public function CustomscodeService(Request $request)
    {
        try {
            //获取参数
            $serviceType = $request->param('serviceType');
            $data = $request->post();
            
            //验证token
            $auth = JWTAuth::auth();
            $this->merchantInfo = json_decode(json_encode($auth['merchantInfo']),true);//验证token

            //方法分发
            switch ($serviceType)
            {
                //海关编码查询
                case 'hgCode':
                    $result = $this->hgCode($data);
                    break;
                default:
                    $result = ['code'=>Code::FAILED, 'msg'=>'未定义的服务'];
            }
            if($result['code'] === 10000){
                return CatchResponse::success($result['data'], $result['msg'], $result['code']);
            }else{
                return CatchResponse::fail($result['msg'],$result['code']);
            }
        } catch (\Exception $exception) {
            return CatchResponse::fail('查询失败');
        }
    }
    /**
     * 海关编码查询
     * @param  $data
     * @return array
     */
    protected function hgCode($data)
    {
         $keys = ['name'];
         $value = $this->check_fields($data, $keys, false);//验证字段
        //判断参数
        if(!is_array($value)){
            return [ 'code'=>Code::FAILED , 'msg'=> $value];
        }
        try {
            if(array_key_exists('name', $data)){
                $name = $value['name'];
            }
            $res = Db::table('db_customs_code')
                ->field('goods_code,goods_name,mfn_taxrate,ordinary_taxrate,increment_taxrate,unit1,unit2')
                ->where('goods_code|goods_name','like','%'.$name.'%')
                ->select()
                ->toArray();

            foreach ($res as $key => $value){
                //法定计量单位/法定第二计量单位
                $res[$key]['unit1Name'] = $this->encoded($value['unit1'],3);
                $res[$key]['unit2Name'] = $this->encoded($value['unit2'],3);
            }
            if(!empty($res)){
                return [ 'code'=> Code::SUCCESS , 'msg'=> '查询成功', 'data'=> $res];
            }else{
                return [ 'code'=>Code::FAILED , 'msg'=> '暂无数据'];
            }
        } catch (\Exception $e) {
            return [ 'code'=>Code::FAILED , 'msg'=> $e->getMessage()];
        }
    }

    /**
     * 从文件中找到编码
     * @time 2021/12/27 16:17
     */
    public function encoded($str,$type)
    {
        $label = str_replace(" ",'',$str);
        if($type === 1){
            //币制
            $path = "json/currency.json";
        } else if($type === 2){
           //国家
            $path = "json/tradeCountry.json";
        } else if($type === 3){
           //单位
            $path = "json/unit.json";
        }
        $json_string = file_get_contents($path);

        $arr = json_decode($json_string,true);

        foreach ($arr as $k => $v) {
            if($v['label'] === $label || $v['value'] === $label){
                return $v['label'];
            }
        }
        return $label;
    }
}